package cn.stronglink.asset.terminal.bjc.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.stronglink.asset.model.AssetOut;
import cn.stronglink.asset.model.Preset;
import cn.stronglink.asset.model.PullOff;
import cn.stronglink.asset.model.PutOn;
import cn.stronglink.asset.model.UsingApply;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.terminal.bjc.log.annotation.PdaLog;
import cn.stronglink.asset.terminal.bjc.log.annotation.PdaLog.OpType;
import cn.stronglink.asset.terminal.bjc.model.PatrolVo;
import cn.stronglink.asset.terminal.bjc.model.UploadInventoryParam;
import cn.stronglink.asset.terminal.bjc.service.PdaService;
import cn.stronglink.suite.core.base.AbstractController;

/**
 * @author sunlei:
 * @version 创建时间：2018年1月3日 下午12:22:05 类说明 北京海关手持机 接口controller
 */

@Controller
@RequestMapping(value = "/bjc")
public class PdaController extends AbstractController {
	@Autowired
	private PdaService pdaService;

	/**
	 * 手持机 同步 基础数据 接口
	 */
	@RequestMapping(value = "queryBaseData")
	@ResponseBody
	public Map<String, Object> queryBaseData(@RequestBody Map<String, String> param) {
		Map<String, Object> map = new HashMap<String, Object>();
		String flag = param.get("param");
		try {
			if ("asset_room".equals(flag)) {// 机房
				map.put("resultCode", 1);
				map.put("resultObj", pdaService.getRooms());
				System.out.println();
			} else if ("status".equals(flag)) {// 设备状态
				map.put("resultCode", 1);
				map.put("resultObj", pdaService.getStatus());
			} else if ("asset_category".equals(flag)) {// 设备类型
				map.put("resultCode", 1);
				map.put("resultObj", pdaService.getAssetType());
			} else if ("workstation".equals(flag)) {// 工作区域 or 数据中心
				map.put("resultCode", 1);
				map.put("resultObj", pdaService.getDataCenter());
			} else if ("asset_vendor".equals(flag)) {// 供应商 目前没有
				map.put("resultCode", 1);
				// map.put("resultObj", pdaService.getAllRack());
			} else if ("cabinet".equals(flag)) {// 机柜
				map.put("resultCode", 1);
				map.put("resultObj", pdaService.getAllRack());// 机柜
			} else if ("assetModel".equals(flag)) {
				map.put("resultCode", 1);
				map.put("resultObj", pdaService.getModel());
			} else if ("deptment".equals(flag)) {
				map.put("resultCode", 1);
				map.put("resultObj", pdaService.getDeptment());
			} else if ("user".equals(flag)) {
				map.put("resultCode", 1);
				map.put("resultObj", pdaService.getUser());
			}
		} catch (

		Exception e) {
			map.put("resultCode", 0);
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 设备查看接口 sn
	 */
	@RequestMapping(value = "getAsset")
	@ResponseBody
	public ResponseEntity<ModelMap> getAsset(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, String> param) {
		List<ItAssetVo> result = null;
		if (param != null) {
			String snCode = param.get("serialNo");
			String rfidCode = param.get("rfidCode");
			String assetCode = param.get("assetNo");
			if (!"".equals(snCode) && !"".equals(rfidCode)) {
				result = pdaService.getAssetBySN(snCode);
			}
			if (!"".equals(snCode) && "".equals(rfidCode)) {
				result = pdaService.getAssetBySN(snCode);
			}
			if ("".equals(snCode) && !"".equals(rfidCode)) {
				result = pdaService.getAssetByRfid(rfidCode);
			}
			if(!"".equals(assetCode) && assetCode !=null){
				result = pdaService.getAssetByAssetCode(assetCode);
			}
		}
		return setSuccessModelMap(modelMap, result);
	}

	/**
	 * 发卡接口
	 */
	@RequestMapping(value = "bindRfid")
	@PdaLog(module = "手持发卡",desc="手持机给设备发卡", type = OpType.UPDATE)
	@ResponseBody
	public ResponseEntity<ModelMap> bindRfid(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> param) {
		System.out.println("-----doing----bindRfidService-");
		String rfid = param.get("rfid").toString();
		String assetId = param.get("assetId").toString();
		pdaService.bindRfid(assetId, rfid);
		return setSuccessModelMap(modelMap, null);
	}

	/**
	 * 设备到货接口
	 */
	@RequestMapping(value = "assetArrival")
	@ResponseBody
	public ResponseEntity<ModelMap> assetArrival(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Preset preset) {
		pdaService.assetArrival(preset);
		return setSuccessModelMap(modelMap, null);
	}

	/**
	 * 同步验收单接口
	 */
	@RequestMapping(value = "downloadPreset")
	@ResponseBody
	public ResponseEntity<ModelMap> downloadPreset(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {
		return setSuccessModelMap(modelMap, pdaService.getPresetList());
	}

	/**
	 * 上传验收单接口
	 */
	@RequestMapping(value = "uploadPreset")
	@ResponseBody
	@PdaLog(module = "上传验收单",desc="手持机上传验收单", type = OpType.UPDATE)
	public ResponseEntity<ModelMap> uploadPreset(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody List<Preset> presetList) {
		pdaService.uploadPreset(presetList);
		return setSuccessModelMap(modelMap, null);
	}

	/**
	 * 设备上架
	 */
	@RequestMapping(value = "assetPutOn")
	@ResponseBody
	public ResponseEntity<ModelMap> assetPutOn(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody PutOn putOn) {
		pdaService.assetPutOn(putOn);
		return setSuccessModelMap(modelMap, null);
	}

	/**
	 * 设备下架
	 */
	@RequestMapping(value = "assetPullOff")
	@ResponseBody
	public ResponseEntity<ModelMap> assetPullOff(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody PullOff pullOff) {
		pdaService.assetPullOff(pullOff);
		return setSuccessModelMap(modelMap, null);
	}

	/**
	 * 设备出库
	 */
	@RequestMapping(value = "assetOut")
	@ResponseBody
	public ResponseEntity<ModelMap> assetOut(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody AssetOut assetOut) {
		pdaService.assetOut(assetOut);
		return setSuccessModelMap(modelMap, null);
	}

	/**
	 * 设备领用
	 */
	@RequestMapping(value = "assetUse")
	@ResponseBody
	public ResponseEntity<ModelMap> assetUse(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody UsingApply using) {
		pdaService.assetUsingApply(using);
		return setSuccessModelMap(modelMap, null);
	}

	/**
	 * 下载盘点
	 */
	@RequestMapping(value = "qryInventory")
	@ResponseBody
	public ResponseEntity<ModelMap> qryInventory(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {
		return setSuccessModelMap(modelMap, pdaService.qryInventorySheetList());
	}

	/**
	 * 上传盘点结果
	 */
	@RequestMapping(value = "uploadInventory")
	@ResponseBody
	@PdaLog(module = "上传盘点结果",desc="手持机上传盘点结果", type = OpType.UPDATE)
	public ResponseEntity<ModelMap> uploadInventory(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody List<UploadInventoryParam> paramList) {
		if (paramList != null && paramList.size() > 0) {
			pdaService.uploadInventory(paramList);
		}
		return setSuccessModelMap(modelMap, null);
	}

	/**
	 * 下载巡检单
	 */
	@RequestMapping(value = "qryPatrol")
	@ResponseBody
	public ResponseEntity<ModelMap> qryPatrol(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {
		return setSuccessModelMap(modelMap, pdaService.qryPatrol(params));
	}

	/**
	 * 上传巡检单
	 */
	@RequestMapping(value = "uploadPatrol")
	@ResponseBody
	@PdaLog(module = "上传巡检单",desc="手持机上传巡检单", type = OpType.UPDATE)
	public ResponseEntity<ModelMap> uploadPatrol(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody List<PatrolVo> patroVoList) {
		pdaService.uploadPatrol(patroVoList);
		return setSuccessModelMap(modelMap, null);
	}

	/**
	 * 下载上架单
	 */
	@RequestMapping(value = "getPutON")
	@ResponseBody
	public ResponseEntity<ModelMap> getUp(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response,
			@RequestBody Map<String, String> params) {
		return setSuccessModelMap(modelMap, pdaService.getPutOnList(params));
	}

	/**
	 * 上传上架单
	 */
	@RequestMapping(value = "uploadPutOn")
	@ResponseBody
	@PdaLog(module = "上传上架单",desc="手持机上传上架单", type = OpType.UPDATE)
	public ResponseEntity<ModelMap> uploadPutOn(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody List<PutOn> putOnList) {
		pdaService.uploadPutOn(putOnList);
		return setSuccessModelMap(modelMap, null);
	}

	/**
	 * 下载下架单
	 */
	@RequestMapping(value = "getPullOff")
	@ResponseBody
	public ResponseEntity<ModelMap> getPullOff(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, String> params) {
		return setSuccessModelMap(modelMap, pdaService.getPullOffList(params));
	}

	/**
	 * 上传下架单
	 */
	@RequestMapping(value = "uploadPullOff")
	@ResponseBody
	@PdaLog(module = "上传下架单",desc="手持机上传下架单", type = OpType.UPDATE)
	public ResponseEntity<ModelMap> uploadPullOff(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody List<PullOff> pullOffList) {
		pdaService.uploadPullOff(pullOffList);
		return setSuccessModelMap(modelMap, null);
	}

}
